#import "WalkScreen.h";
#import "State.h";
#import "WalkMap.h"
#import "Entity.h"
#import "Tile.h"
#import "Shop.h"
#import "Item.h"
#import "Gateway.h"
#import "Buffer.h"

@interface WalkScreen(UtilityMethods)
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center;
@end

#define ZOOM_STEP 1.5
@implementation WalkScreen 

@synthesize tiles,tileSet,spriteSet,spriteSize,tileSize,people,player;
@synthesize shops,shopSet,itemSet,items,gateways,gatewaySet;
-(id) initWithFrame:(CGRect)f buffer:(Buffer*)buffer{

 	[super initWithFrame:f];
    scrollscreen = [[UIScrollView alloc]initWithFrame:self.bounds];
	[scrollscreen setDelegate:self];
	[scrollscreen setBouncesZoom:YES];
	[self addSubview:scrollscreen];
    state=S_MAP_TOWN;	
	[scrollscreen addSubview:map];
 	[self initWithProperties:buffer.stageName];
    
	return self;

  }


#pragma mark screen
-(int)stateMachine{
 int dx = goal.x -player.center.x; 
 int dy = goal.y -player.center.y;
 int tileX=player.center.x/tileSize; 
 int tileY=player.center.y/tileSize;
 
 if (dx >10){
     if([map getType:tileX+1 y:tileY]==TILE_EMPTY) {
 dx =10;
 dy=0;
 player.movement=MOVE_RIGHT;
 }else if([map getType:tileX+1 y:tileY]==TILE_GATEWAY){
 state = S_MAP_DUNGEON;
 
 }
 
 }else if(dx < -10){
     if([map getType:tileX-1 y:tileY]==TILE_EMPTY){
 //move left
 dx=-10;
 dy=0;
 player.movement=MOVE_LEFT;
     }
 }else{
 dx=0;
 if (dy >10){
     if([map getType:tileX y:tileY+1]==TILE_EMPTY) {
 //move down
 dy =10;
 player.movement=MOVE_DOWNWARD;
 }else if (dy < -10)
     if([map getType:tileX y:tileY-1]==TILE_EMPTY){
 //move up
 dy=-10;
 player.movement=MOVE_UPWARD;
     }
 
 }
 else {
 dy=0;
 }
 
 }
 player.center=CGPointMake(player.center.x+dx,player.center.y+dy);
 if(dx==0 && dy==0)
 goal=player.center;
 
 
 
 [self setNeedsDisplay];
 [player setNeedsDisplay];
 for(int i=0;i<[self.people count];i++)
 {
 Entity* e= [people objectAtIndex:i];
 [e setNeedsDisplay];
 
 }
    return state;
}


#pragma mark -

-(id) initWithProperties:(NSString*)name{
	NSString *plistPath = [[NSBundle mainBundle] pathForResource:name ofType:@"plist"];
	NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
	map = [[WalkMap alloc ] initWithBackground:[dic objectForKey:@"background"]];
    [scrollscreen addSubview:map];
    [scrollscreen setContentSize:map.frame.size];
    float minimumScale = [scrollscreen frame].size.width  / [map frame].size.width;
    [scrollscreen setMinimumZoomScale:minimumScale];
    [scrollscreen setZoomScale:minimumScale];
    [map setDelegate:self];

	self.backgroundColor=[UIColor colorWithWhite:1.0 alpha:1.0];
	[back retain];

	[self initTiles:dic];
	[self initPeople:dic];
	[self initShops:dic];
	[self initItems:dic];
	[self initGateways:dic];
	
	player = [[Entity alloc] initWithFrame:CGRectMake(0, 0, spriteSize, spriteSize)];
	[player initWithDictionary:[dic objectForKey:@"player"] ];
	player.set = spriteSet;
	[map addSubview:player];
	return self;
}

-(void)initTiles:(NSMutableDictionary*) dic {
  	tileSize=[[dic objectForKey:@"tileSize"] intValue];
	self.tileSet = [[TileSet alloc] init:[dic objectForKey:@"tileSet"] andSize:tileSize];
    NSMutableArray* g = [dic objectForKey:@"tiles"];
	self.tiles  =[NSMutableArray arrayWithCapacity:[g count]];
    for(int i=0;i<[g count];i++){
		Tile* t = [[Tile alloc] initWithDictionary:[g objectAtIndex:i] ];
		t.set = tileSet;
 		[self.tiles addObject:t];
		[map addSubview:t];
		[map setType:TILE_WALL x:t.x y:t.y];
    }
}
-(void)initPeople:(NSMutableDictionary*) dic {
  	spriteSize=[[dic objectForKey:@"spriteSize"] intValue];
	self.spriteSet = [[TileSet alloc] init:[dic objectForKey:@"spriteSet"] andSize:spriteSize];
    NSMutableArray* ppl = [dic objectForKey:@"people"];
	self.people  =[NSMutableArray arrayWithCapacity:[ppl count]];
    for(int i=0;i<[ppl count];i++){
		Entity* t = [[Entity alloc] initWithDictionary:[ppl objectAtIndex:i]];
		[self.people addObject:t];
		t.set = spriteSet;
		[map addSubview:t];
        [map setType:TILE_WALL x:t.x y:t.y];
    }
}
-(void)initShops:(NSMutableDictionary*) dic {
 	self.shopSet = [[TileSet alloc] init:[dic objectForKey:@"shopSet"] andSize:spriteSize];
    NSMutableArray* ppl = [dic objectForKey:@"shops"];
	self.shops  =[NSMutableArray arrayWithCapacity:[ppl count]];
    for(int i=0;i<[ppl count];i++){
 		Shop* t = [[Shop alloc]initWithDictionary:[ppl objectAtIndex:i]];
		[self.shops addObject:t];
		t.set = shopSet;
		[map addSubview:t];
[map setType:TILE_WALL x:t.x y:t.y];        
    }
}
-(void)initItems:(NSMutableDictionary*) dic {
  	itemHeight=[[dic objectForKey:@"itemHeight"] intValue];
  	itemWidth=[[dic objectForKey:@"itemWidth"] intValue];
	self.itemSet = [[TileSet alloc] init:[dic objectForKey:@"itemSet"] andSize:itemWidth];
    NSMutableArray* ppl = [dic objectForKey:@"items"];
	self.items  =[NSMutableArray arrayWithCapacity:[ppl count]];
    for(int i=0;i<[ppl count];i++){
 		Item* t = [[Item alloc]initWithDictionary:[ppl objectAtIndex:i]];
		[self.items addObject:t];
		[map addSubview:t];
		t.set = itemSet;
        
[map setType:TILE_WALL x:t.x y:t.y];        
    }
}
-(void)initGateways:(NSMutableDictionary*) dic {
  	itemHeight=[[dic objectForKey:@"itemHeight"] intValue];
  	itemWidth=[[dic objectForKey:@"itemWidth"] intValue];
	self.itemSet = [[TileSet alloc] init:[dic objectForKey:@"itemSet"] andSize:itemWidth];
    NSMutableArray* ppl = [dic objectForKey:@"gateways"];
	self.gateways  =[NSMutableArray arrayWithCapacity:[ppl count]];
    for(int i=0;i<[ppl count];i++){
 		Gateway* t = [[Gateway alloc]initWithDictionary:[ppl objectAtIndex:i]];
		[self.gateways addObject:t];
		[map addSubview:t];
		t.set = itemSet;
[map setType:TILE_GATEWAY   x:t.x y:t.y];		
    }
}

-(void)movePalayerTo:(CGPoint)g{
	goal = g;

}
#pragma mark TapWalkMap methods

- (void)tapWalkMap:(WalkMap *)view gotSingleTapAtPoint:(CGPoint)tapPoint {
    [self movePalayerTo:tapPoint];
	[self.player setNeedsDisplay];
}

- (void)tapWalkMap:(WalkMap *)view gotDoubleTapAtPoint:(CGPoint)tapPoint {
    // double tap zooms in
    float newScale = [scrollscreen zoomScale] * ZOOM_STEP;
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:tapPoint];
    [scrollscreen zoomToRect:zoomRect animated:YES];
    [scrollscreen setNeedsDisplay];
}

- (void)tapWalkMap:(WalkMap *)view gotTwoFingerTapAtPoint:(CGPoint)tapPoint {
    // two-finger tap zooms out
    float newScale = [scrollscreen zoomScale] / ZOOM_STEP;
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:tapPoint];
    [scrollscreen zoomToRect:zoomRect animated:YES];
}
#pragma mark Utility methods

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {
    
    CGRect zoomRect;
    
    // the zoom rect is in the content view's coordinates. 
    //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
    //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
    zoomRect.size.height = [self frame].size.height / scale;
    zoomRect.size.width  = [self frame].size.width  / scale;
    
    // choose an origin so as to get the right center.
    zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);
    
    return zoomRect;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return map;
}

@end

